﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Nodes;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WBInfo
{
    public class JJInfoAction
    {
        public static Dictionary<string,object> getAGuNowData()
        {
            Dictionary<string,object> outAry = new Dictionary<string, object>();
            TTCJdata(outAry);
            return outAry;
        }
        
        public static Dictionary<string,object> getFundNowData(string code)
        {
            Dictionary<string,object> outAry = new Dictionary<string, object>();
            Funddata(code,outAry);
            return outAry;
        }
        
        /*聚合数据接口对接,免费版有请求上限*/
        private static void JHdata(Dictionary<string,object> outAry)
        {
            string appKey = "";
            try
            {
                string gid = "";
                
                string type = "0";
                string json = HttpClient.HttpGet("http://web.juhe.cn:8080/finance/stock/hs?gid="+gid+"&type="+type+"&key="+appKey);
                SharesRoot rt = JsonConvert.DeserializeObject<SharesRoot>(json);
                
                outAry.Add("shIncrePer",rt.result.increPer);
                outAry.Add("shName",rt.result.name);
                
                type = "1";
                json = HttpClient.HttpGet("http://web.juhe.cn:8080/finance/stock/hs?gid="+gid+"&type="+type+"&key="+appKey);
                rt = JsonConvert.DeserializeObject<SharesRoot>(json);
                
                outAry.Add("szIncrePer",rt.result.increPer);
                outAry.Add("szName",rt.result.name);
                
                outAry.Add("result","success");
            }
            catch (Exception e)
            {
                outAry.Add("result",e.Message);
                throw;
            }
        }
        /*天天财经的数据爬取没有上限但是不稳定*/
        private static void TTCJdata(Dictionary<string,object> outAry)
        {
            try
            {
                string json = HttpClient.HttpGet("http://50.push2.eastmoney.com/api/qt/clist/get?fltt=2&pn=1&fs=i:1.000001,i:0.399001&fields=f3,f14");
                Dictionary<string,object> rt = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);
                JObject tem =(JObject)(((JObject)((JObject)rt["data"])["diff"])["0"]);
                outAry.Add("shIncrePer",tem.Value<string>("f3"));
                outAry.Add("shName",tem.Value<string>("f14"));
                
                tem =(JObject)(((JObject)((JObject)rt["data"])["diff"])["1"]);
                outAry.Add("szIncrePer",tem.Value<string>("f3"));
                outAry.Add("szName",tem.Value<string>("f14"));
                
                outAry.Add("result","success");
            }
            catch (Exception e)
            {
                outAry.Add("result",e.Message);
            }
        }

        private static void Funddata(string code,Dictionary<string,object> outAry)
        {
            try
            {
                string json = HttpClient.HttpGet("http://fundgz.1234567.com.cn/js/"+code+".js");
                json = json.Replace("jsonpgz(", "").Replace(");", "");
                Dictionary<string,object> rt = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);
                outAry.Add("name",rt["name"]);
                outAry.Add("fundcode",rt["fundcode"]);
                outAry.Add("gszzl",rt["gszzl"]);
                outAry.Add("gztime",rt["gztime"]);
                
                outAry.Add("result","success");
            }
            catch (Exception e)
            {
                outAry.Add("result",e.Message);
            }
        }
    }
}